package tpdisenio2009.mvp.display;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import tpdisenio2009.model.beans.Score;
import tpdisenio2009.model.beans.SoccerMatch;
import tpdisenio2009.model.beans.SoccerPlayer;
import tpdisenio2009.model.beans.TournamentDate;

/**
 *
 * @author Nicolás Kreiff
 */
public class ManageTournamentDatePointsViewImpl2 extends javax.swing.JDialog implements ManageTournamentDatePointsView {

    private HashMap<String, Score> updatedScores = new HashMap<String, Score>();
    private final HashMap<String, String> states = new HashMap<String, String>();
    private SoccerPlayer currentPlayerScore;

    /** Creates new form ManageTournamentDatePointsViewImpl2 */
    public ManageTournamentDatePointsViewImpl2(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        Dimension dim = this.getToolkit().getScreenSize();
        this.setLocation((dim.width - this.getWidth()) / 2,(dim.height - this.getHeight()) / 2);
        
        states.put( SoccerMatch.NON_PLAYED_MATCH, SoccerMatch.PLAYED_MATCH );
        states.put( SoccerMatch.PLAYED_MATCH, SoccerMatch.POSTPONED_MATCH );
        states.put( SoccerMatch.POSTPONED_MATCH, SoccerMatch.SUSPENDED_MATCH );
        states.put( SoccerMatch.SUSPENDED_MATCH, SoccerMatch.NON_PLAYED_MATCH );

        jTableEquipoLocal.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (jTableEquipoLocal.getSelectedRow() >= 0)
                    jTableEquipoVisitante.clearSelection();
            }
        });

        jTableEquipoVisitante.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (jTableEquipoVisitante.getSelectedRow() >= 0)
                    jTableEquipoLocal.clearSelection();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelFecha = new javax.swing.JLabel();
        jComboBoxFecha = new javax.swing.JComboBox();
        jButtonCerrarSesion = new javax.swing.JButton();
        jScrollPanePartidos = new javax.swing.JScrollPane();
        jTablePartidos = new javax.swing.JTable();
        jButtonVerDetalles = new javax.swing.JButton();
        jPanelDetallePartido = new javax.swing.JPanel();
        jLabelEqujpoLocal = new javax.swing.JLabel();
        jLabelLocal = new javax.swing.JLabel();
        jScrollPaneEquipoLocal = new javax.swing.JScrollPane();
        jTableEquipoLocal = new javax.swing.JTable();
        jScrollPaneEquipoVisitante = new javax.swing.JScrollPane();
        jTableEquipoVisitante = new javax.swing.JTable();
        jLabelEquipoVisitante = new javax.swing.JLabel();
        jLabelVisitante = new javax.swing.JLabel();
        jLabelVS = new javax.swing.JLabel();
        jPanelDatosJugador = new javax.swing.JPanel();
        jScrollPaneDatosJugador = new javax.swing.JScrollPane();
        jTableDatosJugador = new javax.swing.JTable();
        jButtonGuardar = new javax.swing.JButton();
        jButtonSiguiente = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tpdisenio2009.Tpdisenio2009App.class).getContext().getResourceMap(ManageTournamentDatePointsViewImpl2.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabelFecha.setText(resourceMap.getString("jLabelFecha.text")); // NOI18N
        jLabelFecha.setName("jLabelFecha"); // NOI18N

        jComboBoxFecha.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19" }));
        jComboBoxFecha.setName("jComboBoxFecha"); // NOI18N

        jButtonCerrarSesion.setText(resourceMap.getString("jButtonCerrarSesion.text")); // NOI18N
        jButtonCerrarSesion.setName("jButtonCerrarSesion"); // NOI18N

        jScrollPanePartidos.setName("jScrollPanePartidos"); // NOI18N

        jTablePartidos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Partido", "Local", "Visitante", "Estado (*)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTablePartidos.setName("jTablePartidos"); // NOI18N
        jTablePartidos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPanePartidos.setViewportView(jTablePartidos);
        jTablePartidos.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTablePartidos.columnModel.title0")); // NOI18N
        jTablePartidos.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTablePartidos.columnModel.title1")); // NOI18N
        jTablePartidos.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTablePartidos.columnModel.title2")); // NOI18N
        jTablePartidos.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTablePartidos.columnModel.title3")); // NOI18N

        jButtonVerDetalles.setText(resourceMap.getString("jButtonVerDetalles.text")); // NOI18N
        jButtonVerDetalles.setName("jButtonVerDetalles"); // NOI18N

        jPanelDetallePartido.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanelDetallePartido.border.title"))); // NOI18N
        jPanelDetallePartido.setName("jPanelDetallePartido"); // NOI18N

        jLabelEqujpoLocal.setText(resourceMap.getString("jLabelEqujpoLocal.text")); // NOI18N
        jLabelEqujpoLocal.setName("jLabelEqujpoLocal"); // NOI18N

        jLabelLocal.setText(resourceMap.getString("jLabelLocal.text")); // NOI18N
        jLabelLocal.setName("jLabelLocal"); // NOI18N

        jScrollPaneEquipoLocal.setName("jScrollPaneEquipoLocal"); // NOI18N

        jTableEquipoLocal.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Puesto", "Jugador", "Nro. Camiseta"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableEquipoLocal.setName("jTableEquipoLocal"); // NOI18N
        jTableEquipoLocal.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPaneEquipoLocal.setViewportView(jTableEquipoLocal);
        jTableEquipoLocal.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTableEquipoLocal.columnModel.title0")); // NOI18N
        jTableEquipoLocal.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTableEquipoLocal.columnModel.title1")); // NOI18N
        jTableEquipoLocal.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTableEquipoLocal.columnModel.title2")); // NOI18N

        jScrollPaneEquipoVisitante.setName("jScrollPaneEquipoVisitante"); // NOI18N

        jTableEquipoVisitante.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Puesto", "Jugador", "Nro. Camiseta"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableEquipoVisitante.setName("jTableEquipoVisitante"); // NOI18N
        jTableEquipoVisitante.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPaneEquipoVisitante.setViewportView(jTableEquipoVisitante);
        jTableEquipoVisitante.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTableEquipoVisitante.columnModel.title0")); // NOI18N
        jTableEquipoVisitante.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTableEquipoVisitante.columnModel.title1")); // NOI18N
        jTableEquipoVisitante.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTableEquipoVisitante.columnModel.title2")); // NOI18N

        jLabelEquipoVisitante.setText(resourceMap.getString("jLabelEquipoVisitante.text")); // NOI18N
        jLabelEquipoVisitante.setName("jLabelEquipoVisitante"); // NOI18N

        jLabelVisitante.setText(resourceMap.getString("jLabelVisitante.text")); // NOI18N
        jLabelVisitante.setName("jLabelVisitante"); // NOI18N

        jLabelVS.setFont(resourceMap.getFont("jLabelVS.font")); // NOI18N
        jLabelVS.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelVS.setText(resourceMap.getString("jLabelVS.text")); // NOI18N
        jLabelVS.setName("jLabelVS"); // NOI18N

        javax.swing.GroupLayout jPanelDetallePartidoLayout = new javax.swing.GroupLayout(jPanelDetallePartido);
        jPanelDetallePartido.setLayout(jPanelDetallePartidoLayout);
        jPanelDetallePartidoLayout.setHorizontalGroup(
            jPanelDetallePartidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDetallePartidoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelDetallePartidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelDetallePartidoLayout.createSequentialGroup()
                        .addComponent(jLabelEqujpoLocal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelLocal, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
                    .addComponent(jScrollPaneEquipoLocal, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelVS, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelDetallePartidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelDetallePartidoLayout.createSequentialGroup()
                        .addComponent(jLabelEquipoVisitante)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelVisitante, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPaneEquipoVisitante, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10))
        );
        jPanelDetallePartidoLayout.setVerticalGroup(
            jPanelDetallePartidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDetallePartidoLayout.createSequentialGroup()
                .addGroup(jPanelDetallePartidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelDetallePartidoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelDetallePartidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelDetallePartidoLayout.createSequentialGroup()
                                .addGroup(jPanelDetallePartidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabelEqujpoLocal)
                                    .addComponent(jLabelLocal, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPaneEquipoLocal, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanelDetallePartidoLayout.createSequentialGroup()
                                .addGroup(jPanelDetallePartidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabelVisitante, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabelEquipoVisitante, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPaneEquipoVisitante, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanelDetallePartidoLayout.createSequentialGroup()
                        .addGap(117, 117, 117)
                        .addComponent(jLabelVS)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelDatosJugador.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanelDatosJugador.border.title"))); // NOI18N
        jPanelDatosJugador.setName("jPanelDatosJugador"); // NOI18N

        jScrollPaneDatosJugador.setName("jScrollPaneDatosJugador"); // NOI18N

        jTableDatosJugador.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Puntos", "G.Hechos", "G.Recibidos", "Amarilla", "Roja", "Penales Hechos", "Penales Errados", "Penales Atajados", "Goles en Contra", "Figura", "Tiempo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Boolean.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTableDatosJugador.setName("jTableDatosJugador"); // NOI18N
        jScrollPaneDatosJugador.setViewportView(jTableDatosJugador);

        javax.swing.GroupLayout jPanelDatosJugadorLayout = new javax.swing.GroupLayout(jPanelDatosJugador);
        jPanelDatosJugador.setLayout(jPanelDatosJugadorLayout);
        jPanelDatosJugadorLayout.setHorizontalGroup(
            jPanelDatosJugadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDatosJugadorLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneDatosJugador, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelDatosJugadorLayout.setVerticalGroup(
            jPanelDatosJugadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDatosJugadorLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneDatosJugador, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButtonGuardar.setText(resourceMap.getString("jButtonGuardar.text")); // NOI18N
        jButtonGuardar.setName("jButtonGuardar"); // NOI18N

        jButtonSiguiente.setText(resourceMap.getString("jButtonSiguiente.text")); // NOI18N
        jButtonSiguiente.setName("jButtonSiguiente"); // NOI18N

        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPanePartidos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 737, Short.MAX_VALUE)
                    .addComponent(jPanelDatosJugador, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabelFecha)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 566, Short.MAX_VALUE)
                        .addComponent(jButtonCerrarSesion))
                    .addComponent(jPanelDetallePartido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButtonSiguiente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonGuardar))
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonVerDetalles, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelFecha)
                    .addComponent(jComboBoxFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonCerrarSesion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPanePartidos, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonVerDetalles)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelDetallePartido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelDatosJugador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonGuardar)
                    .addComponent(jButtonSiguiente))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrarSesion;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonSiguiente;
    private javax.swing.JButton jButtonVerDetalles;
    private javax.swing.JComboBox jComboBoxFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelEquipoVisitante;
    private javax.swing.JLabel jLabelEqujpoLocal;
    private javax.swing.JLabel jLabelFecha;
    private javax.swing.JLabel jLabelLocal;
    private javax.swing.JLabel jLabelVS;
    private javax.swing.JLabel jLabelVisitante;
    private javax.swing.JPanel jPanelDatosJugador;
    private javax.swing.JPanel jPanelDetallePartido;
    private javax.swing.JScrollPane jScrollPaneDatosJugador;
    private javax.swing.JScrollPane jScrollPaneEquipoLocal;
    private javax.swing.JScrollPane jScrollPaneEquipoVisitante;
    private javax.swing.JScrollPane jScrollPanePartidos;
    private javax.swing.JTable jTableDatosJugador;
    private javax.swing.JTable jTableEquipoLocal;
    private javax.swing.JTable jTableEquipoVisitante;
    private javax.swing.JTable jTablePartidos;
    // End of variables declaration//GEN-END:variables

    public void setDates(List<TournamentDate> dates) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (TournamentDate date : dates)
            model.addElement( date );
        jComboBoxFecha.setModel( model );
    }

    public TournamentDate getDate() {
        DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxFecha.getModel();
        return (TournamentDate) model.getSelectedItem();
    }

    public void setFirstOpenDate( TournamentDate date ) {
        jComboBoxFecha.getModel().setSelectedItem(date);
    }

    public void addDateChangeListener(ActionListener listener) {
        jComboBoxFecha.addActionListener( listener );
    }

    public void removeDateChangeListener(ActionListener listener) {
        jComboBoxFecha.removeActionListener( listener );
    }

    public void addCloseSessionListener(ActionListener listener) {
        jButtonCerrarSesion.addActionListener(listener);
    }

    public void removeCloseSessionListener(ActionListener listener) {
        jButtonCerrarSesion.removeActionListener(listener);
    }

    public void loadMatchs(final List<SoccerMatch> matchs) {
        SoccerMatchTableModel model = new SoccerMatchTableModel(matchs);
        jTablePartidos.setModel(model);
    }

    public SoccerMatch getSelectedMatch() {
        SoccerMatchTableModel model = (SoccerMatchTableModel) jTablePartidos.getModel();
        return model.getSoccerMatch( jTablePartidos.getSelectedRow() );
    }

    public void disableMatchTable() {
        jTablePartidos.setEnabled(false);
    }

    public void disableHomeTable() {
        jTableEquipoLocal.setEnabled(false);
    }

    public void disableVisitorTable() {
        jTableEquipoVisitante.setEnabled(false);
    }

    public void enableMatchTable() {
        jTablePartidos.setEnabled(true);
    }

    public void enableHomeTable() {
        jTableEquipoLocal.setEnabled(true);
    }

    public void enableVisitorTable() {
        jTableEquipoVisitante.setEnabled(true);
    }

    public List<SoccerMatch> getMatchs() {
        SoccerMatchTableModel model = (SoccerMatchTableModel) jTablePartidos.getModel();
        return model.getMatchs();
    }

    public void addStateChangedListener(ActionListener listener) {
        jTablePartidos.addMouseListener( new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = jTablePartidos.getColumnModel().getColumnIndexAtX( e.getX() );
                    if (index == 3) {
                        SoccerMatchTableModel model = (SoccerMatchTableModel) jTablePartidos.getModel();
                        int row = jTablePartidos.getSelectedRow();
                        SoccerMatch match = model.getSoccerMatch(row);
                        match.setState( states.get( match.getState() ) );
                        jTablePartidos.repaint();
                    }
                }
            }
        });
    }

    public void removeStateChangedListener(ActionListener listener) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addShowDetailsListener(ActionListener listener) {
        jButtonVerDetalles.addActionListener(listener);
    }

    public void removeShowDetailsListener(ActionListener listener) {
        jButtonVerDetalles.removeActionListener(listener);
    }

    public void setLocalTeamName(String localTeamName) {
        jLabelLocal.setText( localTeamName );
    }

    public void setVisitorTeamName(String visitorTeamName) {
        jLabelVisitante.setText( visitorTeamName );
    }

    public void loadLocalPlayers(List<SoccerPlayer> players) {
        SoccerPlayerTableModel model = new SoccerPlayerTableModel( players );
        jTableEquipoLocal.setModel(model);
    }

    public void loadVisitorPlayers(List<SoccerPlayer> players) {
        SoccerPlayerTableModel model = new SoccerPlayerTableModel( players );
        jTableEquipoVisitante.setModel(model);
    }

    public void setFirstPlayerSelected() {
        jTableEquipoLocal.getSelectionModel().clearSelection();
        jTableEquipoVisitante.getSelectionModel().clearSelection();
        if (jTableEquipoLocal.getModel().getRowCount() > 0) {
            jTableEquipoLocal.getSelectionModel().setSelectionInterval( 0, 0 );
            showPoints();
        }
    }

    public void showPoints() {
        jPanelDatosJugador.setVisible(true);
    }

    public void hidePoints() {
        jPanelDatosJugador.setVisible(false);
    }

    public SoccerPlayer getSelectedPlayer() {
        if (jTableEquipoLocal.getSelectedRow() >= 0) {
            SoccerPlayerTableModel model = (SoccerPlayerTableModel) jTableEquipoLocal.getModel();
            return model.getSoccerPlayer( jTableEquipoLocal.getSelectedRow() );
        }
        if (jTableEquipoVisitante.getSelectedRow() >= 0) {
            SoccerPlayerTableModel model = (SoccerPlayerTableModel) jTableEquipoVisitante.getModel();
            return model.getSoccerPlayer( jTableEquipoVisitante.getSelectedRow() );
        }
        return null;
    }

    public void addPlayerSelectionListener( ListSelectionListener listener ) {
        jTableEquipoLocal.getSelectionModel().addListSelectionListener(listener);
        jTableEquipoVisitante.getSelectionModel().addListSelectionListener(listener);
    }

    public void removePlayerSelectionListener( ListSelectionListener listener ) {
        jTableEquipoLocal.getSelectionModel().removeListSelectionListener(listener);
        jTableEquipoVisitante.getSelectionModel().removeListSelectionListener(listener);
    }

    public void setPlayerScore(Score score) {
        DefaultTableModel model = (DefaultTableModel) jTableDatosJugador.getModel();

        if (currentPlayerScore != null) {
            Score currentScore = updatedScores.get( String.valueOf( currentPlayerScore.getId() ) );

            System.err.println("UPDATED-SCORES: " + updatedScores.keySet());
            System.err.println("CURRENT-PLAYER-SCORE: " + currentPlayerScore + (currentScore == null? " NULL" : " NOT-NULL"));

            if (currentScore == null && !updatedScores.containsKey( String.valueOf( currentPlayerScore.getId() ) )) {
                currentScore = new Score();
                updatedScores.put( String.valueOf( currentPlayerScore.getId() ), currentScore);
                System.err.println("PUT: " + currentPlayerScore);
            }

            currentScore.setScore( getInt( model.getValueAt( 0, 0 ) ) );
            currentScore.setGoals( getInt( model.getValueAt( 0, 1 ) ) );
            currentScore.setReceivedGoals( getInt( model.getValueAt( 0, 2 ) ) );
            currentScore.setYellowCard( getBool( model.getValueAt( 0, 3 ) ) );
            currentScore.setRedCard( getBool( model.getValueAt( 0, 4 ) ) );
            currentScore.setPenalties( getInt( model.getValueAt( 0, 5 ) ) );
            currentScore.setFailedPenalties( getInt( model.getValueAt( 0, 6 ) ) );
            currentScore.setCatchedPenalties( getInt( model.getValueAt( 0, 7 ) ) );
            currentScore.setOwnGoals( getInt( model.getValueAt( 0, 8 ) ) );
            currentScore.setFigure( getBool( model.getValueAt( 0, 9 ) ) );
            currentScore.setTime( getInt( model.getValueAt( 0, 10 ) ) );
            currentScore.setSoccerPlayer( currentPlayerScore );
            currentScore.setTournamentDate( getDate() );
        }

        currentPlayerScore = getSelectedPlayer();

        jTableDatosJugador.clearSelection();
        TableCellEditor editor = jTableDatosJugador.getCellEditor();
        if (editor != null)
            editor.cancelCellEditing();

        model.setRowCount( 0 );
        model.setRowCount( 1 );

        if (score == null) {
            score = updatedScores.get( String.valueOf( currentPlayerScore.getId() ) );
            if (score == null) {
                for (int i=0; i<model.getColumnCount(); i++)
                    model.setValueAt( null, 0, i );
                return;
            }
        }

        model.setValueAt( getInt( score.getScore() ), 0, 0);
        model.setValueAt( getInt( score.getGoals() ), 0, 1);
        model.setValueAt( getInt( score.getReceivedGoals() ), 0, 2);
        model.setValueAt( getBool( score.getYellowCard() ), 0, 3);
        model.setValueAt( getBool( score.getRedCard() ), 0, 4);
        model.setValueAt( getInt( score.getPenalties() ), 0, 5);
        model.setValueAt( getInt( score.getFailedPenalties() ), 0, 6);
        model.setValueAt( getInt( score.getCatchedPenalties() ), 0, 7);
        model.setValueAt( getInt( score.getOwnGoals() ), 0, 8);
        model.setValueAt( getBool( score.getFigure() ), 0, 9);
        model.setValueAt( getInt( score.getTime() ), 0, 10);

        if (score != null)
            updatedScores.put( String.valueOf( currentPlayerScore.getId() ), score);
    }

    private int getInt( Object intObj ) {
        try {
            return Integer.parseInt( intObj.toString() );
        } catch (Exception ex) {
            return 0;
        }
    }

    private boolean getBool( Object boolObj ) {
        try {
            return ((Boolean) boolObj).booleanValue();
        } catch (Exception ex) {
            return false;
        }
    }

    public Collection<Score> getUpdatedScores() {
        return updatedScores.values();
    }

    public void addSaveListener(ActionListener listener) {
        jButtonGuardar.addActionListener(listener);
    }

    public void removeSaveListener(ActionListener listener) {
        jButtonGuardar.removeActionListener(listener);
    }

    public void showError( String errorMessage, String titleMessageWindow) {
        JOptionPane.showMessageDialog(this, errorMessage, titleMessageWindow, JOptionPane.ERROR_MESSAGE);
    }

    public void showOK(String okMessage, String titleMessageWindow) {
        JOptionPane.showMessageDialog(null, okMessage, titleMessageWindow, JOptionPane.INFORMATION_MESSAGE);
    }

    public boolean showConfirmation( String notifiactionMessage ) {
        int confirm = JOptionPane.showConfirmDialog( this, notifiactionMessage, "Confirmación", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE );

        if (JOptionPane.YES_OPTION == confirm)
            return true;
        else
            return false;
    }
    
    public Component asWidget() {
        return this;
    }

}
